package com.gy.smart.util;


import android.content.Context;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;

import java.util.ArrayList;
import java.util.List;

/**
 *  定位类
 *  调用addLocationListener 开始定位
 *  需实现 OnLocationListener接收定位结果
 * */
public class LocationClient implements AMapLocationListener {

    private static LocationClient client = null;
    public AMapLocationClient mLocationClient = null;
    public AMapLocationClientOption mLocationOption = null;
    private List<OnLocationListener> locationListeners = new ArrayList<OnLocationListener>();
    private boolean startLocation = false;//是否开始定位

    private LocationClient(Context context){
        mLocationClient = new AMapLocationClient(context);
        mLocationClient.setLocationListener(this);

        //初始化定位参数
        mLocationOption = new AMapLocationClientOption();
        mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        //设置是否返回地址信息（默认返回地址信息）
        mLocationOption.setNeedAddress(true);
        //设置是否只定位一次,默认为false
        mLocationOption.setOnceLocation(false);
        //设置是否强制刷新WIFI，默认为强制刷新
        mLocationOption.setWifiActiveScan(true);
        //设置是否允许模拟位置,默认为false，不允许模拟位置
        mLocationOption.setMockEnable(false);
        //设置定位间隔,单位毫秒,默认为2000ms
        mLocationOption.setInterval(2000);
        //给定位客户端对象设置定位参数
        mLocationClient.setLocationOption(mLocationOption);
    }

    public static LocationClient getInstance(Context context){
        if (client == null){
            client = new LocationClient(context);
        }
        return client;
    }

    /** 开始定位*/
    public void startLocation(){
        if(!startLocation){
            startLocation = true;
            mLocationClient.startLocation();
        }
    }

    /** 添加定位监听*/
    public void addLocationListener(OnLocationListener listener){
        if(listener == null){
            throw new NullPointerException("OnLocationListener is null");
        }
        locationListeners.add(listener);
        startLocation();
    }

    @Override
    public void onLocationChanged(AMapLocation aMapLocation) {
        startLocation = false;//定位结束
        if(aMapLocation.getErrorCode() == 0){
            if(aMapLocation != null){
                for (OnLocationListener listener : locationListeners){
                    listener.onAmapLocationListener(aMapLocation);
                }
                locationListeners.clear();
            }else {
                for (OnLocationListener listener : locationListeners){
                    listener.onAmapLocationFail();
                }
            }
            mLocationClient.stopLocation();
        } else {
//            LogUtil.e("定位失败："+aMapLocation.getErrorInfo());
        }
    }

    /**
     * 接收定位结果
     */
    public interface OnLocationListener {
        public void onAmapLocationListener(AMapLocation aMapLocation);
        public void onAmapLocationFail();
    }
}
